﻿using log4net;
using log4net.Config;
using log4net.Repository;

namespace LowCoding.Common
{
    public class LogHelper
    {
        private static ILoggerRepository repository { get; set; }
        private static ILog _loginfo;
        private static ILog _logsql;
        private static ILog _logerror;
        private static ILog _loglogin;
        private static ILog _loghttp;

        private static ILog loginfo
        {
            get
            {
                if (_loginfo == null)
                    Configure();
                return _loginfo;
            }
        }

        private static ILog logsql
        {
            get
            {
                if (_logsql == null)
                    Configure();
                return _logsql;
            }
        }

        private static ILog logerror
        {
            get
            {
                if (_logerror == null)
                    Configure();
                return _logerror;
            }
        }

        private static ILog loglogin
        {
            get
            {
                if (_loglogin == null)
                    Configure();
                return _loglogin;
            }
        }

        private static ILog loghttp
        {
            get
            {
                if (_loghttp == null)
                    Configure();
                return _loghttp;
            }
        }

        public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "log4net.config")
        {
            repository = LogManager.CreateRepository(repositoryName);
            XmlConfigurator.Configure(repository, new FileInfo(configFile));
            _loginfo = LogManager.GetLogger(repositoryName, "loginfo");
            _logsql = LogManager.GetLogger(repositoryName, "logsql");
            _logerror = LogManager.GetLogger(repositoryName, "logerror");
            _loglogin = LogManager.GetLogger(repositoryName, "loglogin");
            _loghttp = LogManager.GetLogger(repositoryName, "loghttp");
        }

        /// <summary>
        /// 记日志
        /// </summary>
        /// <param name="info"></param>
        public static void WriteLog(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }

        public static void WriteSqlLog(string info)
        {
            if (logsql.IsInfoEnabled)
            {
                logsql.Info(info);
            }
        }

        public static void WriteHttplog(string info)
        {
            if (loghttp.IsInfoEnabled)
            {
                loghttp.Info(info);
            }
        }

        public static void WriteLoginlog(string info)
        {
            if (loglogin.IsInfoEnabled)
            {
                loglogin.Info(info);
            }
        }

        /// <summary>
        /// 写异常日志
        /// </summary>
        /// <param name="info"></param>
        /// <param name="se"></param>
        public static void WriteErrorLog(string info)
        {
            logerror.Error(info);
        }

        public static void WriteErrorLog(Exception exinfo)
        {
            if (exinfo != null)
                logerror.Error(exinfo);
        }
    }
}